// recordPackage/pages/rankingList/rankingList.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    selectDate: '2024-04-19',
    visitList: [],
    saleList: [],
    activeFlag: false

  },

  // 切换顶部导航栏
  changeTab(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      activeFlag: flag == 'visit'
    })
  },


  getRecord() {
    var url = app.globalData.url + "customer/getUserChildrenVisit",
      that = this,
      visitList = [],
      saleList = [];
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        datetime: that.data.selectDate
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data

          wx.hideLoading()
          console.log(datas);

          var arr = datas.children;
          for (var i in arr) {
            arr[i].invalidNum = 0; //无效
            arr[i].validNum = 0; //有效
            arr[i].sale_num = 0;
            for (var j in datas.visitList) {
              if (arr[i].user_id == datas.visitList[j].user_id) {
                if (datas.visitList[j].isValid == '有效') {
                  arr[i].validNum = datas.visitList[j].visit_num;
                }
                if (datas.visitList[j].isValid == '无效') {
                  arr[i].invalidNum = datas.visitList[j].visit_num;
                }
              }
            }
            for (var j in datas.salesList) {
              if (arr[i].user_id == datas.salesList[j].user_id) {
                arr[i].sale_num = datas.salesList[j].totalNum;
              }
            }
          }
          var arr1 = JSON.parse(JSON.stringify(arr))
          saleList = arr.sort(that.compareSales());
          visitList = arr1.sort(that.compare());



          that.setData({
            visitList,
            saleList
          })
          console.log(that.data);

        }
      },
      err => {
        console.log(err);
      })
  },
  compareSales() {
    return function (value1, value2) {
      var val1 = value1['sale_num'];
      var val2 = value2['sale_num'];
      return Number(val2) - Number(val1);
    }
  },
  compare() {
    return function (value1, value2) {
      var val1 = value1['validNum'] + value1['invalidNum'];
      var val2 = value2['validNum'] + value2['invalidNum'];
      return Number(val2) - Number(val1);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})